package com.higgins.eventtracking.client.health.overview;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.EventTrackingService;
import com.higgins.eventtracking.client.EventTrackingServiceAsync;
import com.higgins.eventtracking.client.health.Fundamentals;

public class HygieneTab extends VerticalPanel	{
	private final EventTrackingServiceAsync greetingService = GWT
										.create(EventTrackingService.class);
	private Label hoursSinceBrushTeeth = new Label();
	private Label numberOfBrushesToday= new Label();
	private boolean firstRun = true;
	private Boolean alert1 = null;
	private Fundamentals parentTab = null;
	
	public HygieneTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		setHeight("20em");
	}
	
	public HygieneTab()	{
		
	}
	
	public void update()	{
		if (firstRun)
			initialise();
	}
	public void initialise()	{
		setLabelWidths();
		addFields();
		populateFields();
		firstRun = false;
	}
	public void addFields()	{
		HorizontalPanel hygieneRowOne = new HorizontalPanel();
		hygieneRowOne.setSpacing(3);
		
		hygieneRowOne.add(hoursSinceBrushTeeth);
		setCellHeight(hoursSinceBrushTeeth, Integer.toString(hoursSinceBrushTeeth.getElement().getOffsetHeight())+"px");
		
		hygieneRowOne.add(numberOfBrushesToday);
		setCellHeight(numberOfBrushesToday, Integer.toString(hoursSinceBrushTeeth.getElement().getOffsetHeight())+"px");
		
		add(hygieneRowOne);
		//setHeight("100%");
	}
	private void setLabelWidths()	{
		hoursSinceBrushTeeth.setWidth("250px");
		numberOfBrushesToday.setWidth("250px");
	}
	private void populateFields()	{
		populateHoursSinceBrushedTeeth();
		populateNumberOfBrushesToday();
	}
	private void populateHoursSinceBrushedTeeth()	{
		greetingService.retrieveHoursSinceBrushTeeth(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
						alert1 = false;
//						passBackToParent();

					}
					public void onSuccess(final Integer results) {
						hoursSinceBrushTeeth.setText(results.toString() + " hours since last brushed teeth");
						if (results!=null && results > 5)	{
							hoursSinceBrushTeeth.addStyleName("highlightAlertLabel");
							alert1 = true;
						} else {
							alert1 = false;
						}
//						passBackToParent();

					}
				});
	}
	private void populateNumberOfBrushesToday()	{
		greetingService.retrieveNumberOfBrushesToday(
				new AsyncCallback<Integer>() {
					public void onFailure(Throwable caught) {
//						runCharts(new ArrayList<HealthStatsDTO>());
					}
					public void onSuccess(final Integer results) {
						numberOfBrushesToday.setText(results!=null?results.toString() + " brushes today":"");
					}
				});
	}	
//	private void passBackToParent()	{
//		if (alert1!=null)	{
//			parentTab.addHygieneTab(alert1);
//		}
//	}
}
